//
//  YanTitleButton.swift
//  SinaWeibo
//
//  Created by mac on 2017/5/18.
//  Copyright © 2017年 mac. All rights reserved.
//

import UIKit

class YanTitleButton: UIButton {

    // 重载构造函数
    /// title如果是nil,就显示首页
    /// 如果不为nil，显示title和箭头图像
    init(title: String?) {
        super.init(frame: CGRect())
        
        // 1.判断title是否为nil
        if title == nil {
            setTitle("首页", for: [])
        } else {
            setTitle(title! + " ", for: [])
            // 设置图像
            setImage(UIImage(named: "navigationbar_arrow_down"), for: [])
            setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        }
        
        // 2.设置字体和颜色
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        
        // 3.设置大小
        sizeToFit()
 
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let titleLabel = titleLabel, let imageView = imageView else {
            return
        }
        
        // OC中不允许直接修改结构体内部的值
        // Swift中可以直接修改
        
        titleLabel.frame.origin.x = 0

        imageView.frame.origin.x = titleLabel.bounds.width
    }
    
}
